package com.app.micaihu.view.login.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.infor.UserInfor
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.ConfigKeyConstant
import com.app.micaihu.configure.TypeConstant
import com.app.micaihu.manager.AppConfigManager
import com.app.micaihu.view.login.contract.LogInContract
import com.app.micaihu.view.login.presenter.LogInPresenter
import com.app.micaihu.view.user.userinfo.activity.WebActivity
import com.app.micaihu.view.utils.loadCircleImage
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.login_bind_phone_activity.*

/**
 * Created by Guo on 2016/7/24.
 * 绑定手机
 */
class BindPhoneByCodeActivity : BaseMvpActivity<LogInContract.View, LogInPresenter>(), LogInContract.View {

    override fun getPresenter() = LogInPresenter()

    override fun getLayoutId() = R.layout.login_bind_phone_activity

    override fun setData() {
        val userInfoStr = Biz.instance.userInfo
        if (userInfoStr.isNotEmpty()) {
            GsonUtils.fromJson<UserInfor>(userInfoStr, UserInfor::class.java)?.let {
                ivHead.loadCircleImage(it.headPic, R.mipmap.image_default_round)
                tvNickName.text = it.nickName
            }
        }
    }

    override fun addListeners() {
        etPhone.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                tvLoginCode.isEnabled = etPhone.text.toString().isNotEmpty()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
        tvLoginCode.setOnClickListener {
            mPresenter?.sendCode(etPhone.text.toString(), TypeConstant.BINDPHONE_CODE)
        }
        tvUserProtocol.setOnClickListener {
            //用户协议
            val userAgreementUrl = AppConfigManager.getInstance().configSP.getString(ConfigKeyConstant.userAgreementUrl, "")
            val bundle = Bundle()
            bundle.putString(Config.Extra.url, userAgreementUrl)
            enterPager(WebActivity::class.java, bundle)
        }
        tvPrivacyProtocol.setOnClickListener {
            //隐私协议
            val privateAgreementUrl = AppConfigManager.getInstance().configSP.getString(ConfigKeyConstant.privateAgreementUrl, "")
            val bundle = Bundle()
            bundle.putString(Config.Extra.url, privateAgreementUrl)
            enterPager(WebActivity::class.java, bundle)
        }
    }

    override fun handleSendCode(result: BaseBean<Any?>?) {
        hideLoading()
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                val bundle = Bundle()
                bundle.putString(Config.Extra.phone, etPhone.text.toString())
                enterPager(BindPhoneByCodeConfirmActivity::class.java, bundle)
            } else {
                ToastUtils.showShort(result.msg)
            }
        }

    }
}